#!/usr/bin/env
#coding=utf-8

import socket
host, port = '', 50010
s = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
s.connect((host, port))

while 1:
    fn = raw_input('input your filename: ').strip()
    infile, outfile = fn.split()[:2]
    s.sendall('get\t%s' % infile)

    buf = ''
    len_data = 0
    len_buf = 0
    ofh = open(outfile, 'wb+')
    while 1:
        tmp = s.recv(1024)
        if not tmp:
            break
        if not len_data:
            str_len, str_tmp = tmp.split('\t', 1)
            len_data = int(str_len)
            ofh.write(str_tmp)
            len_buf += len(str_tmp)
        else:
            ofh.write(tmp)
            len_buf += len(tmp)
        print len_buf, len_data, 100 * len_buf / len_data
        # ofh.write('%s'% buf)
        if len_buf == len_data:
            break
    print 'congratulations, your file %s has been received successfully,file len %s' % (fn, len_data)
    ofh.close()